package com.example.sunsg.okhttptest.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.customview.HorizenScrollView;
import com.example.sunsg.okhttptest.util.DisplayUtils;


public class HorizontalScrollViewWithListViewActivity extends Activity {
    private int mScreenWidth;
    private HorizenScrollView mSview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horizontal_scroll_view_with_list_view);
        mSview = ((HorizenScrollView) findViewById(R.id.container));
        mScreenWidth = DisplayUtils.getScreenWidth(getApplicationContext());
        init();
    }

    private void init(){
        addChilderView();
    }

    private void addChilderView(){
        addChiliedListView(MyAdapter.TYPE_1);
        addChiliedListView(MyAdapter.TYPE_2);
        addChiliedListView(MyAdapter.TYPE_3);
    }

    private void addChiliedListView(int type){
        ListView lv1 = new ListView(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(mScreenWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        lv1.setLayoutParams(params);
        setListViewType(lv1);
        lv1.setAdapter(new MyAdapter(type));
        mSview.addView(lv1);
    }

    private void setListViewType(ListView listView){
        listView.setBackgroundColor(0x00000000);
        listView.setCacheColorHint(0x00000000);
        listView.setDividerHeight(0);
    }

    class MyAdapter extends BaseAdapter{
        public static final int TYPE_1 = 1;
        public static final int TYPE_2 = 2;
        public static final int TYPE_3 = 3;

        private int type;
        public  MyAdapter(int type){
            this.type = type;
        }

        @Override
        public int getCount() {
            return 20;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final HolderView holderView;
            if(convertView == null){
                convertView = View.inflate(getApplicationContext(),R.layout.horizontal_scroll_view_with_listview_activity_item,null);
                holderView = new HolderView();
                holderView.btn = ((Button) convertView.findViewById(R.id.btn_click));
                holderView.llContainer = ((LinearLayout) convertView.findViewById(R.id.ll_container));
                convertView.setTag(holderView);
            }else{
                holderView = ((HolderView) convertView.getTag());
            }
            holderView.btn.setText(position +" sunsg");
            switch (type){
                case TYPE_1:
                    holderView.btn.setBackgroundColor(0xffff0000);
                    holderView.llContainer.setBackgroundColor(0xffff0000);
                    break;
                case TYPE_2:
                    holderView.btn.setBackgroundColor(0xff00ff00);
                    holderView.llContainer.setBackgroundColor(0xff00ff00);
                    break;
                case TYPE_3:
                    holderView.btn.setBackgroundColor(0xff0000ff);
                    holderView.llContainer.setBackgroundColor(0xff0000ff);
                    break;
            }
            return convertView;
        }
    }

    class HolderView{
        Button btn;
        LinearLayout llContainer;
    }


}
